package name.sakanacatcher.controller;

import name.sakanacatcher.entity.Advertisement;
import name.sakanacatcher.repository.AdRepository;
import name.sakanacatcher.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
@RestController
@RequestMapping("/api/ad")
public class AdController {

    @Autowired
    ResultUtil resultUtil;

    @Autowired
    AdRepository adRepository;

    @GetMapping("/name/{name}")
    Map<String, Object> getAd(@PathVariable String name) {
        return resultUtil.successResult("get success",Map.of("items", adRepository.getByName(name)));
    }

    @GetMapping("/list")
    Map<String, Object> getAdList() {
        return resultUtil.successResult("get success",Map.of("items", adRepository.findAll()));
    }
    @PostMapping("/update")
    Map<String, Object> setAd(@RequestBody Advertisement advertisement){
        adRepository.save(advertisement);
        return resultUtil.successResult("update success");
    }

}
